package com.servlets;

import com.domain.Movies;
import com.domain.service.MoviesService;
import com.domain.utils.GetConnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UpdateMovieServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String movieName = request.getParameter("movieName");


        MoviesService moviesService = new MoviesService();

        List<Movies> moviesList = moviesService.getAll();

        Movies movies = null;


        for (int i = 0; i < moviesList.size(); i++) {

            if (moviesList.get(i).getMovieName().equals(movieName)) {
                movies = moviesList.get(i);
            }
        }


        response.setContentType("text/html");

        PrintWriter out = null;

        out = response.getWriter();

        try {

            out.println("<html>");
            out.println("<title>AbhiApp | MoviesList</title>");
            out.println("<head></head>");
            out.println("<body bgcolor='gray'>");

            out.println("<form method='post' action='UpdateMovieServlet'>");

            out.println("<table bgcolor='white'align='center' cellspacing='40' >");

            out.println("<tr><td>Enter the movie name</td><td><input type='text' name='movieName' value='" + movieName + "'></td></tr>");

            out.println("<tr><td>Select a language</td><td><select name='language'>");
            out.println("<option value='" + movies.getLanguage() + "'>" + movies.getLanguage() + "</option>");
            out.println("<option value='Telugu'>Telugu</option>");
            out.println("<option value='English'>English</option>");
            out.println("<option value='Hindi'>Hindi</option>");
            out.println("</select></td></tr>");

            out.println("<tr><td>Select a category</td><td><select name='category'>");
            out.println("<option value='" + movies.getCategory() + "'> " + movies.getCategory() + "</option>");
            out.println("<option value='General'>General</option>");
            out.println("<option value='Horror'>Horror</option>");
            out.println("<option value='Fiction'>Fiction</option>");
            out.println("<option value='Family'>Family</option>");
            out.println("<option value='Love Story'>Love Story</option>");
            out.println("</select></td></tr>");

            out.println("<tr><td>Enter the description</td><td><textarea value='" + movies.getDescription() + "'name='description'></textarea></td></tr>");

            out.println("<tr><td>Enter the year</td><td><input type='text' value='" + movies.getYear() + "' name='year'></td></tr>");
            out.println("<tr><td></td><td><input type='submit' value='update'></td></tr>");

            out.println("</table>");

            out.println("</form>");

            out.println("</body>");
            out.println("</html>");

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

            if (out != null) {
                out.close();
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException,
            IOException {


        String movieName = request.getParameter("movieName");
        String year = request.getParameter("year");
        String description = request.getParameter("description");
        String category = request.getParameter("category");
        String language = request.getParameter("language");


        Connection connection = null;

        PreparedStatement preparedStatement = null;

        try {

            connection = GetConnection.setConnection();

            preparedStatement = connection.prepareStatement("Update MOVIES set language=?,category=?,description=?,movieName=?,year=? WHERE movieName=?");

            preparedStatement.setString(1, language);
            preparedStatement.setString(2, category);
            preparedStatement.setString(3, description);
            preparedStatement.setString(4, movieName);
            preparedStatement.setString(5, year);

            preparedStatement.setString(6, movieName);

            preparedStatement.executeUpdate();

            connection.close();

        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }

        response.sendRedirect("MoviesListServlet");
    }
}
